package io.renren.controller;

import io.renren.entity.LeaveEntity;
import io.renren.entity.SysUserEntity;
import io.renren.service.LeaveService;
import io.renren.utils.PageUtils;
import io.renren.utils.QueryAuthority;
import io.renren.utils.R;
import io.renren.utils.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * 请假
 * 
 * @author zhangheng
 * @email zh_123shmily@163.com
 * @date 2017-07-22 12:54:48
 */
@RestController
@RequestMapping("leave")
public class LeaveController extends  AbstractController{
	@Autowired
	private LeaveService leaveService;
	
	/**
	 * 列表
	 */
	@RequestMapping("/list")
	@RequiresPermissions("leave:list")
	public R list(@RequestParam Map<String, Object> params){
		//查询列表数据
		QueryAuthority query = new QueryAuthority(params);

		List<LeaveEntity> leaveList = leaveService.queryList(query);
		int total = leaveService.queryTotal(query);
		
		PageUtils pageUtil = new PageUtils(leaveList, total, query.getLimit(), query.getPage());
		
		return R.ok().put("page", pageUtil);
	}
	
	
	/**
	 * 信息
	 */
	@RequestMapping("/info/{id}")
	@RequiresPermissions("leave:info")
	public R info(@PathVariable("id") Integer id){
		LeaveEntity leave = leaveService.queryObject(id);
		
		return R.ok().put("leave", leave);
	}
	
	/**
	 * 保存
	 */
	@RequestMapping("/save")
	@RequiresPermissions("leave:save")
	public R save(@RequestBody LeaveEntity leave){
		SysUserEntity entity=getUser();
		leave.setCdate(new Date());
		leave.setCusername(entity.getUsername());
		leave.setUserdesc(entity.getUserdesc());
		leave.setCompany(entity.getCompany());
		leave.setCompanyId(entity.getCompanyId());
		leave.setDeptId(entity.getDeptNo());
		leave.setDept(entity.getDept());
		leave.setPost(entity.getPost());
		leaveService.save(leave);
		
		return R.ok();
	}
	
	/**
	 * 修改
	 */
	@RequestMapping("/update")
	@RequiresPermissions("leave:update")
	public R update(@RequestBody LeaveEntity leave){
		leaveService.update(leave);
		
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	@RequiresPermissions("leave:delete")
	public R delete(@RequestBody Integer[] ids){
		leaveService.deleteBatch(ids);
		
		return R.ok();
	}


	/**
	 * 提交
	 */
	@RequestMapping("/submit")
	@RequiresPermissions("leave:submit")
	public R submit(@RequestBody Integer id){
		LeaveEntity leave = leaveService.queryObject(id);
		String msg=leaveService.submit(leave);
		if(StringUtils.isNotBlank(msg)){
			return R.error(1, msg);
		}
		return R.ok();
	}


	/**
	 * 批准
	 */
	@RequestMapping("/process")
	public R process(@RequestBody LeaveEntity leave){

		String loginer=getUser().getUsername()==null?"":getUser().getUsername();
		if(!loginer.equals(leave.getNextUser())){
			return R.error(2,"当前登录人非流程下个审批人，不能审批");
		}

		leaveService.process(leave);

		return R.ok();
	}

	/**
	 * 拒绝
	 */
	@RequestMapping("/reject")
	public R reject(@RequestBody LeaveEntity leave){

		String loginer=getUser().getUsername()==null?"":getUser().getUsername();
		if(!loginer.equals(leave.getNextUser())){
			return R.error(2,"当前登录人非流程下个审批人，不能审批");

		}
		leaveService.reject(leave);

		return R.ok();
	}
	
}
